package com.krmes.mes;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.krmes.mes.api.remote.ServerOkHttpApi;
import com.krmes.mes.app.AppContext;
import com.krmes.mes.app.AppManager;
import com.krmes.mes.util.TDevice;
import com.krmes.mes.util.Utils;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cz.msebera.android.httpclient.Header;


public class LoginActivity extends FragmentActivity
        implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        AppManager.getAppManager().addActivity(this);

        PackageManager pm = this.getPackageManager();
        PackageInfo pi;
        versionName = "";
        try {
            pi = pm.getPackageInfo(this.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            versionName="";
        }
        initView();

    }
    private Button btn_login;
    private EditText et_password;
    private EditText et_userid;
    private ImageView iv_logo;
    private int login_count = 0;
    private String versionName;

    private void initView()
    {
        this.btn_login = ((Button)findViewById(R.id.btn_login));
        this.btn_login.setOnClickListener(this);
        this.et_userid = ((EditText)findViewById(R.id.et_userid));
        this.et_password = ((EditText)findViewById(R.id.et_password));
        this.iv_logo = ((ImageView)findViewById(R.id.iv_logo));
        this.iv_logo.setOnClickListener(this);
    }

    private boolean validate()
    {
        if ((this.et_userid.getText() == null) || (this.et_userid.getText().toString().equals("")))
        {
            AppContext.showToast("请输入用户名");
            this.et_userid.requestFocus();
            return false;
        }
        if ((this.et_password.getText() == null) || (this.et_password.getText().toString().equals("")))
        {
            AppContext.showToast("请输入密码");
            this.et_password.requestFocus();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View paramView) {
        if (paramView.getId() == R.id.btn_login) {

            this.btn_login.setClickable(false);
            if (Utils.isFastDoubleClick()) {
                AppContext.showToast("点击频率太快了!",0,0, Gravity.BOTTOM);
//                Toast t = new Toast(this);
//                t.setText("点击频率太快了");
//                t.show();
                return;
            }
            if (validate()) {
                if (login_count>0){
                    AppContext.showToast("已经在验证用户密码，请等待服务器返回!",0,0, Gravity.BOTTOM);
                    return;
                }
                login_count ++;
                ServerOkHttpApi.login2(this.et_userid.getText().toString(), this.et_password.getText().toString(), TDevice.getDeviceId(),"app",versionName, new AsyncHttpResponseHandler()
                {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] pArrayOfByte) {
                        try
                        {
                            login_count--;
                            JSONObject jsonResult = new JSONObject(new String(pArrayOfByte, "UTF-8"));
                            int i = jsonResult.optInt("ret_code");
                            String str = jsonResult.optString("err_msg");
                            LoginActivity.this.btn_login.setClickable(true);
                            if (i == 0)
                            {
                                String[] arrayOfString = jsonResult.optString("data").split(",");
                                AppContext.loginCode = arrayOfString[0];
                                AppContext.userName = arrayOfString[1];
                                AppContext.roleCode = arrayOfString[2];
                                AppContext.roleName = arrayOfString[3];
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                LoginActivity.this.startActivity(intent);
                                LoginActivity.this.finish();
                                return;
                            }
                            AppContext.showToast(str);
                            return;
                        }
                        catch (UnsupportedEncodingException localUnsupportedEncodingException)
                        {
                            localUnsupportedEncodingException.printStackTrace();
                            return;
                        }
                        catch (JSONException localJSONException)
                        {
                            localJSONException.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable pThrowable) {
                        login_count--;
                        LoginActivity.this.btn_login.setClickable(true);
                        AppContext.showToast(pThrowable.getMessage());
                        pThrowable.printStackTrace();
                        pThrowable.toString();
                        Log.d("statusCode",String.valueOf(statusCode));

                    }


                });
            }else{
                this.btn_login.setClickable(true);
            }
        }
        if (paramView.getId() == R.id.iv_logo) {
            startActivity(new Intent(this, ConfigActivity.class));
            finish();
            return;
        }

    }
}
